/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-11-26
*/
package com.rzico.account.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.account.entity.FoundationFlow;
import com.rzico.account.service.FoundationFlowService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 基金流水控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-11-26
 */
@Api(description = "基金流水接口")
@RestController
@RequestMapping("/admin/foundationFlow")
public class FoundationFlowAdminController extends BaseController {

    @Autowired
    private FoundationFlowService foundationFlowService;


    @Autowired
    private EnterpriseService enterpriseService;


    /**
     * 分页查询基金流水
     *
     * @return
    */
    @ApiOperation("分页查询基金流水")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "客户ID", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型(1.收入 2.分红)", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "flowType", value = "类型(1.vip1,2.vip2.3.vip3,4.vip4,5.vip5,6.fund,7.atvy,8.bonus)", dataType = "Long", paramType = "query")
    })
    public CommResult<FoundationFlow> list(Date startDate, Date endDate,Long memberId,Integer type,Integer flowType, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (memberId!=null){
            params.put("memberId", memberId);
        }
        if (type!=null){
            params.put("type", type);
        }
        if (flowType!=null){
            params.put("flowType", flowType);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<FoundationFlow> list = foundationFlowService.selectList(params);
        PageResult<FoundationFlow> pageResult = new PageResult<FoundationFlow>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条基金流水
     *
     * @return
    */
    @ApiOperation("查询单条基金流水")
    @GetMapping("/find/{id}")
    public CommResult<FoundationFlow> find(@PathVariable String id) {
        FoundationFlow result = foundationFlowService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("foundationFlow",result);
        return CommResult.success(data);

    }

}
